﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCExtension;
using MVCExtension.Pager;
using FTchinaMVC.Models;

namespace FTchinaMVC.Controllers
{
    public class UserRoleController : BaseController
    {
        private FTchinaMVCContext db = new FTchinaMVCContext();

        //
        // GET: /UserRole/

        public ActionResult Index(int? id)
        {
		   
			 ViewBag.ControllerName = "UserRoles";
			string key = "UserRolesItemsCount";
            if (id == null) { id = 1; }
            int ItemsCount = 0;
            #region 缓存取总数
            if (MVCExtension.FrameWork.CacheHelper.IsExits(key))
            {
                ItemsCount = (int)MVCExtension.FrameWork.CacheHelper.GetObjectCache(key);
            }
            else
            {
                ItemsCount=db.UserRoles.Count();
                MVCExtension.FrameWork.CacheHelper.AddObjectCache(key, ItemsCount);
            }
            #endregion
            ViewBag.ItemsCount = ItemsCount;
            ViewBag.PageSize = 15;
            ViewBag.PageIndex = id; 
            int skip = ((int)id-1) * 15;          
		 
 
            var userroles =  db.UserRoles
											.OrderByDescending(m => m.ID)
											.Skip(skip).Take((int)ViewBag.PageSize);
            return View(userroles.ToList());
 
        }

		
		/// <summary>
        /// 导出到Excel
        /// </summary>
        public void Excel()
        {
            MVCExtension.Office.ExcelHelper.Export2Excel<UserRole>(db.UserRoles.ToList<UserRole>());
        }
		
        //
        // GET: /UserRole/Details/5

        public ActionResult Details(int id = 0)
        {
            UserRole userrole = db.UserRoles.Find(id);
            if (userrole == null)
            {
                return HttpNotFound();
            }
            return View(userrole);
        }

        //
        // GET: /UserRole/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /UserRole/Create

        [HttpPost]
        public ActionResult Create(UserRole userrole)
        {
            if (ModelState.IsValid)
            {
                db.UserRoles.Add(userrole);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(userrole);
        }

        //
        // GET: /UserRole/Edit/5

        public ActionResult Edit(int id = 0)
        {
            UserRole userrole = db.UserRoles.Find(id);
            if (userrole == null)
            {
                return HttpNotFound();
            }
            return View(userrole);
        }

        //
        // POST: /UserRole/Edit/5

        [HttpPost]
        public ActionResult Edit(UserRole userrole)
        {
            if (ModelState.IsValid)
            {
                db.Entry(userrole).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(userrole);
        }

        //
        // GET: /UserRole/Delete/5

        public ActionResult Delete(int id = 0)
        {
            UserRole userrole = db.UserRoles.Find(id);
            if (userrole == null)
            {
                return HttpNotFound();
            }
            return View(userrole);
        }

        //
        // POST: /UserRole/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            UserRole userrole = db.UserRoles.Find(id);
            db.UserRoles.Remove(userrole);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
		
		 /// <summary>
        /// 批量操作
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GroupProcess()
        {

            string tmpWords = string.Empty;
            string optype = MVCExtension.FrameWork.StringHelper.SafeRequest(Request.Form["optype"]);
            switch (optype)
            {
                case "del":
                    {
                        string ids = MVCExtension.FrameWork.StringHelper.SafeRequest(Request.Form["ids"]);
                        foreach (string id in ids.Split(','))
                        {
                            if (id.Trim().Length > 0)
                            {
                                db.UserRoles.Remove(db.UserRoles.Find(Convert.ToInt32(id)));
                                db.SaveChanges();
                            }
                        }
                        tmpWords = "删除" + ids + "成功！";
                        break;
                    }
            }
            return Content(tmpWords);
        }
    }
}